<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />

        <el-tree
          :data="departs"
          :props="defaultPrors"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <popups
      ref="popups"
      :show-dialog.sync="showDialog"
      :tree-node="clickedNode"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
import Popups from './components/popups'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    Popups
  },
  props: {},
  data() {
    return {
      company: {},
      departs: [],
      defaultPrors: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      clickedNode: null
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getDepartments()
  },
  mounted() { },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: '北京雍禾医疗投资管理有限公司', manager: '负责人', id: '' }
      this.departs = tranListToTreeDate(result.depts, '')
      // console.log(this.departs)
    },
    addDepts(node) {
      this.showDialog = true
      this.clickedNode = node
    },
    editDepts(node) {
      this.showDialog = true
      this.clickedNode = node
      // 在当组件获取组件实例调用子类组件的方法
      this.$refs.popups.getDepartDetail(node.id)
    }

  }
}
</script>

<style  scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

